<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>test debounce</title>
</head>

<body>
    <div>
        <button id="bb">test</button>
    </div>
    <script>
        function debunce(fn) {
            let timer;
            return function () {
                if (timer != null) {
                    clearTimeout(timer)
                }
                timer = setTimeout(() => {
                    fn.apply(this, arguments)
                    console.log(' wojwowowoowoo');
                }, 2000)
            }
        }
        function immidiate(fn) {
            let timer;
            return function () {
                if (timer != null) {
                    clearTimeout(timer)
                }
                let callNow = !timer
                timer = setTimeout(() => {
                    timer = null
                }, 2000)
                if (callNow) {
                    fn()
                    console.log('执行完了');
                }

            }
        }
        function throttle(fn, wait) {
            let timer;
            return function () {
                if (!timer) {
                    timer = setTimeout(() => {
                        fn()
                        timer = null
                    }, 1000)
                }
            }
        }
        // 时间戳
        function throttle2(fn, wait = 2000) {
            let prev = 0
            return function () {
                let now = Date.now()
                console.log(now, prev);
                if (now - prev > wait) {
                    fn()
                    prev = now
                }
            }
        }
        function handler() {
            console.log('shayebushi');
        }
        var button = document.getElementById("bb").addEventListener('click', throttle2(handler))
    </script>
</body>

</html>